package com.qf.im.application;

import com.alibaba.druid.spring.boot3.autoconfigure.DruidDataSourceAutoConfigure;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Import;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@SpringBootApplication
@ComponentScan("com.qf")
@MapperScan("com.qf.**.dao")
@Import(DruidDataSourceAutoConfigure.class)
@EnableTransactionManagement
@EnableAspectJAutoProxy
public class ImServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ImServerApplication.class, args);
    }

    /**
     * 解决跨域的过滤器
     * @return
     */
    @Bean
    public CorsFilter corsWebFilter(){
        //跨域的配置对象
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.setAllowCredentials(false);
        corsConfiguration.addAllowedMethod("*");//POST GET
        corsConfiguration.addAllowedOrigin("*");//所有来源
        corsConfiguration.addAllowedHeader("*");//所有请求头都允许跨域

        //创建跨域的配置源对象
        UrlBasedCorsConfigurationSource corsConfigurationSource = new UrlBasedCorsConfigurationSource();
        //配置源中注册配置
        corsConfigurationSource.registerCorsConfiguration("/**",  corsConfiguration);
        return new CorsFilter(corsConfigurationSource);
    }
}
